package com.treasure.service.commons.lang.util;

import java.text.DecimalFormat;

public class MapUtil {
	private static double EARTH_RADIUS = 6378.137;
	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}
	/**
	 * 通过经纬度获取距离(单位：米)
	 */
	public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
		double radLat1 = getRadian(lat1);
		double radLat2 = getRadian(lat2);
		double a = radLat1 - radLat2;// 两点纬度差
		double b = getRadian(lng1) - getRadian(lng2);// 两点的经度差
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1)
				* Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		return getTwoDecimal(s);
	}
	private static double getRadian(double degree) {
		return degree * Math.PI / 180.0;
	}
	/**
	  * 将数据保留两位小数
	  */
     private static double getTwoDecimal(double num) {
          DecimalFormat dFormat=new DecimalFormat("#.00");
          String yearString=dFormat.format(num);
          Double temp= Double.valueOf(yearString);
          return temp;
     }

	public static void main(String[] args) {
		System.out.println(getDistance(39.90871, 116.3975, 39.91445, 116.48487));
	}
}
